package com.chenglai.goods.service;
import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.commonutils.common.OSSUtil;
import com.chenglai.goods.dao.ProductCommentMapper;
import com.chenglai.goods.dao.ProductCommentPicMapper;
import com.chenglai.goods.pojo.ProductComment;
import com.chenglai.goods.pojo.ProductCommentPic;
import com.chenglai.goods.service.CommentService;
import com.sun.corba.se.spi.activation.RepositoryPackage.ServerDefHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jiang
 * @created 2023/11/16 10:06:32
 **/
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private ProductCommentMapper productCommentMapper;
    @Autowired
    private ProductCommentPicMapper productCommentPicMapper;

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void addProductComment(ProductComment productComment) {
        productComment.setCommentTime(DateFormatHelper.currentTimes());
        productCommentMapper.addProductComment(productComment);

        List<String> productCommentPics = productComment.getProductCommentPics();
        List<ProductCommentPic> list = new ArrayList<>();
        for (String productCommentPic : productCommentPics) {
            ProductCommentPic pic = new ProductCommentPic();
            pic.setCommentPic(productCommentPic);
            pic.setCommentId(productComment.getCommentId());
            list.add(pic);
        }
        productCommentPicMapper.insertList(list);

    }

    @Override
    public String addProductCommentPic(HttpServletRequest request) {
        MultipartFile pic = ((MultipartRequest) request).getFile("file");
        return OSSUtil.uploadingImg(pic, "shangcheng/commentPic");
    }

    @Override
    public List<ProductComment> getProductComment(Integer productId) {
        Example example = new Example(ProductComment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId",productId);
        example.orderBy("commentTime").desc();
        List<ProductComment> productCommentList = productCommentMapper.selectByExample(example);
        //user表补全后使用
//        List<ProductComment> productCommentList = productCommentMapper.getProductComment(productId);
        for (ProductComment productComment : productCommentList) {
            Example example1 = new Example(ProductCommentPic.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("commentId",productComment.getCommentId());
            List<ProductCommentPic> list = productCommentPicMapper.selectByExample(example1);
            productComment.setProductCommentPicList(list);
        }
        return productCommentList;
    }

    @Override
    public void delProductComment(Integer commentId, String pics) {
        productCommentMapper.deleteByPrimaryKey(commentId);
        if (!pics.equals("")){
            //删除评论图片
            String[] split = pics.split(",");
            OSSUtil.deleteListFile(Arrays.asList(split));
            Example example = new Example(ProductCommentPic.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("commentId",commentId);
            productCommentPicMapper.deleteByExample(example);
        }
    }
}
